package com.jp.fm.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Map;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jp.fm.HomeFinanceRunner;
import com.jp.fm.data.Category;

public class CategoryDaoTest {

	CategoryDao categoryDao = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		categoryDao = new CategoryDao();

		// Clear cache
		HomeFinanceRunner.getCache(CategoryDao.CACHE_NAME).removeAll();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public final void testFindAll() {
		Set<Category> ret = categoryDao.findAll();
		assertNotNull(ret);
		assertEquals(8, ret.size());

		// Now get from cache
		ret = categoryDao.findAll();
		assertNotNull(ret);
		assertEquals(8, ret.size());
	}

	@Test
	public final void testFindAllKeywordToCategoryMappings() {
		Map<String, String> ret = categoryDao
				.findAllKeywordToCategoryMappings();
		assertNotNull(ret);
		assertEquals(2, ret.size());

		// Now get from cache
		ret = categoryDao.findAllKeywordToCategoryMappings();
		assertNotNull(ret);
		assertEquals(2, ret.size());
	}

	@Test
	public final void testFindByName() {
		Category cat = categoryDao.findByName("Groceries");
		assertNotNull(cat);
		assertEquals(Integer.valueOf(0), cat.getId());

		// Now get from cache
		cat = categoryDao.findByName("Groceries");
		assertNotNull(cat);
		assertEquals(Integer.valueOf(0), cat.getId());
	}

	@Test
	public final void testFindByNameUnknown() {
		Category cat = categoryDao.findByName("test1");
		assertNotNull(cat);
		assertEquals(Integer.valueOf(7), cat.getId());
	}

	@Test
	public final void testFindById() {
		Category cat = categoryDao.findById(0);
		assertNotNull(cat);
		assertEquals("Groceries", cat.getName());

		// Now get from cache
		cat = categoryDao.findById(0);
		assertNotNull(cat);
		assertEquals("Groceries", cat.getName());
	}

}
